# -*- encoding: utf-8 -*-
"""
File Name: generate_import_json
Description:
Author: 'lirui'
date: 6/23/2025
Contact: marjey_lee@163.com
Change Activity:
Date: 6/23/2025:
"""
import ll

a = [
    {
        "所属区域": "互联网业务接入区（西园）",
        "运营商": "电信",
        "IPv4": "116.52.13.33",
        "IPv6": "240e:64d:10:100::2"
    },
    {
        "所属区域": "互联网业务接入区（西园）",
        "运营商": "移动",
        "IPv4": "183.224.181.65",
        "IPv6": "2409:876c:2:1001::2"
    },
    {
        "所属区域": "互联网业务接入区（拓东12F）",
        "运营商": "电信",
        "IPv4": "220.163.86.145",
        "IPv6": "240e:64c:100:1::2"
    },
    {
        "所属区域": "互联网业务接入区（拓东12F）",
        "运营商": "",
        "IPv4": "220.163.106.225",
        "IPv6": ""
    },
    {
        "所属区域": "互联网业务接入区（拓东12F）",
        "运营商": "移动",
        "IPv4": "39.129.8.193",
        "IPv6": "2409:876c:203:40::2"
    },
    {
        "所属区域": "互联网业务接入区（拓东12F）",
        "运营商": "联通",
        "IPv4": "119.62.102.17",
        "IPv6": ""
    },
    {
        "所属区域": "互联网业务接入区（拓东4F）",
        "运营商": "电信",
        "IPv4": "220.163.114.81",
        "IPv6": "240e:64c:160:1::2"
    },
    {
        "所属区域": "互联网业务接入区测试（西园）",
        "运营商": "电信",
        "IPv4": "116.249.139.193",
        "IPv6": "240e:64c:500:1::2"
    },
    {
        "所属区域": "互联网业务接入区测试（西园）",
        "运营商": "移动",
        "IPv4": "218.206.50.33",
        "IPv6": ""
    },
    {
        "所属区域": "互联网业务接入区测试（拓东4F）",
        "运营商": "移动",
        "IPv4": "39.129.31.225",
        "IPv6": ""
    },
    {
        "所属区域": "互联网外联接入区（西园）",
        "运营商": "电信",
        "IPv4": "116.52.3.65",
        "IPv6": ""
    },
    {
        "所属区域": "互联网外联接入区（西园）",
        "运营商": "移动",
        "IPv4": "183.224.181.177",
        "IPv6": ""
    },
    {
        "所属区域": "西园园区外网出口",
        "运营商": "移动",
        "IPv4": "218.206.56.193",
        "IPv6": ""
    }
]

import asyncio


def _generate_icmp_sec_format(area, ip, cooperator):
    name = area + '-' + cooperator + '-' + ip + '-imcp_sec'
    template = {
        "name": name,
        "type": "SIMPLE",
        "key": f"icmppingsec[{ip}]",
        "value_type": "FLOAT",
        "trends": "200d",
        "triggers": [
            {
                "expression": f"last(/互联网专线ICMP监控/icmppingsec[{ip}],#3)>0.5",
                "name": name,
                "priority": "AVERAGE"
            }
        ]
    }
    return template


def _generate_icmp_format(area, ip, cooperator):
    name = area + '-' + cooperator + '-' + ip + '-imcp'
    template = {
        "name": name,
        "type": "SIMPLE",
        "key": f"icmpping[{ip}]",
        "value_type": "FLOAT",
        "trends": "200d",
        "triggers": [
            {
                "expression": f"last(/互联网专线ICMP监控/icmpping[{ip}],#3)<0.5",
                "name": name,
                "priority": "AVERAGE"
            }
        ]
    }
    return template


async def _main():
    items = []
    for item in a:
        area = item['所属区域']
        cooperator = item['运营商']
        ipv4 = item['IPv4']
        ipv6 = item['IPv6']
        if len(ipv4) > 1:
            items.append(_generate_icmp_format(area, ipv4, cooperator))
            items.append(_generate_icmp_sec_format(area, ipv4, cooperator))
        if len(ipv6) > 1:
            items.append(_generate_icmp_format(area, ipv6, cooperator))
            items.append(_generate_icmp_sec_format(area, ipv6, cooperator))
    print(ll.json2str(items))


if __name__ == '__main__':
    asyncio.run(_main())
